<template>
  <div class="app_container">
    <h3 class="module_head">营业收入统计-商家</h3>
    <div class="filter-container" style="margin: 0 auto 20px 0;">
      <el-date-picker
        v-model="dateRange"
        type="daterange"
        align="right"
        style="margin-right: 10px; width: 300px"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions">
      </el-date-picker>
      <el-button type="primary" @click="onFilter" icon="el-icon-search">
        筛选
      </el-button>
    </div>
    <el-table
      :data="list"
      v-loading="listLoading"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      height="350"
      class="record_table">
      <el-table-column align="center" label="编号" width="90px">
        <template slot-scope="scope">
          {{ scope.$index }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="消费机id">
        <template slot-scope="scope">
          {{ scope.row.terminalId }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="消费机">
        <template slot-scope="scope">
          {{ scope.row.terminalName }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="早餐">
        <template slot-scope="scope">
          {{ scope.row.breakfastAmount }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="午餐">
        <template slot-scope="scope">
          {{ scope.row.lunchAmount }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="晚餐">
        <template slot-scope="scope">
          {{ scope.row.dinnerAmount }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="夜宵">
        <template slot-scope="scope">
          {{ scope.row.supperAmount }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="消费次数">
        <template slot-scope="scope">
          {{ scope.row.consumptionTimes }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="总营业额">
        <template slot-scope="scope">
          {{ scope.row.totalAmount }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="日期">
        <template slot-scope="scope">
          {{ scope.row.reportDate }}
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="totalPages>0" :total="totalPages" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="fetchData" ></pagination>
  </div>
</template>

<script>
import { getList } from '@/api/income'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'MerchantStatisticalReport',
  components: { Pagination },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.items
        this.listLoading = false
        this.totalPages = response.data.total
      })
    },
    // 把标准日期转换为yyyy-mm-dd格式的字符串
    dateToString(object) {
      var year = object.getFullYear()
      var month = object.getMonth() + 1
      var day = object.getDate()
      return year + '-' + month + '-' + day
    },
    onFilter() {
      this.listQuery.pageNum = 1
      if (this.dateRange){
        this.listQuery.startDate = this.dateToString(this.dateRange[0])
        this.listQuery.endDate = this.dateToString(this.dateRange[1])
      }
      getList(this.listQuery).then(response => {
        this.list = response.data.items
        this.listLoading = false
        this.totalPages = response.data.total
      })
    }
  },
  data(){
    return{
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      dateRange: null,
      list: null,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        startDate: undefined,
        endDate: undefined
      },
      currentPage: 1,
      totalPages: NaN
    };
  }
}
</script>

<style scoped>
.app_container{
  padding: 20px;
}
.module_head{
  margin-top: 0;
}
.record_table{
  width: 100%;
  margin-bottom: 20px;
}
</style>
